package cn.kgc.controller;

import cn.kgc.base.dto.AdminUserDTO;
import cn.kgc.base.entity.JwtToken;
import cn.kgc.commons.annotation.IgnoreResultAdviceAnnotation;
import cn.kgc.commons.constant.HttpExceptionCodeEnum;
import cn.kgc.commons.exception.HttpException;
import cn.kgc.commons.util.RedisUtil;
import cn.kgc.constant.AuthorityCenterConstant;
import cn.kgc.entity.UmsMember;
import cn.kgc.service.JwtService;
import cn.kgc.service.UmsMemberService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static cn.kgc.commons.constant.HttpExceptionCodeEnum.PHONE_CODE_ERROR;
import static cn.kgc.commons.constant.SystemConstant.REGISTER_PHONE_CODE;

/**
 * @description:
 * @author: 石浩杰
 * @date: 2022-12-02 09:35
 */
@RequestMapping("/auth")
@Api(tags = "认证接口")
@RestController
public class AuthorityController {

    @Resource
    private JwtService jwtService;

    @Resource
    private RedisUtil redisUtil;

    @Resource
    private UmsMemberService umsMemberService;

    @PostMapping("/getToken")
    @ApiOperation(value = "登录，获得令牌",httpMethod = "Post",produces = "application/json;charset=utf-8",response = JwtToken.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "password",value = "密码",dataType = "String",paramType = "query")
    })
    @IgnoreResultAdviceAnnotation
    @CrossOrigin
    public JwtToken getToken(@RequestBody AdminUserDTO adminUserDTO) throws Exception {
        String token = jwtService.token(adminUserDTO.getUsername(),adminUserDTO.getPassword());
        if (token!=null){
            JwtToken jwtToken = new JwtToken();
            jwtToken.setToken(token);
            return jwtToken;
        }
        return null;
    }



    @PostMapping("/register")
    @ApiOperation(value = "注册，获得令牌",httpMethod = "Post",produces = "application/json;charset=utf-8",response = JwtToken.class)
    @CrossOrigin
    @IgnoreResultAdviceAnnotation
    public JwtToken register(@RequestBody AdminUserDTO adminUserDTO)throws Exception{
        //判断账号是否重复
        LambdaQueryWrapper<UmsMember> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UmsMember::getUsername,adminUserDTO.getUsername());
        int count=umsMemberService.count(lambdaQueryWrapper);
        if (count>0){
            throw new HttpException(HttpExceptionCodeEnum.USERNAME_REPEAT.getCode(), HttpStatus.INTERNAL_SERVER_ERROR.value());
        }
        //验证码是否正确
        String key= REGISTER_PHONE_CODE+adminUserDTO.getMobile();
        if (redisUtil.hasKey(key)){
            String tempCode=redisUtil.get(key).toString();
            if (tempCode.equals(adminUserDTO.getCode())){
                String token=jwtService.register(adminUserDTO.getUsername(), adminUserDTO.getPassword());
                JwtToken jwtToken = new JwtToken();
                jwtToken.setToken(token);
                return jwtToken;
            }
        }
        throw new HttpException(PHONE_CODE_ERROR.getCode(), HttpStatus.INTERNAL_SERVER_ERROR.value());
        //注册获得令牌
    }
}
